package com.zcc.controller;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zcc.pojo.User;
import com.zcc.utils.JsonUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
public class UserController {

    //地址
    @RequestMapping("/j1")
    //返回字符串，不走视图解析器，不会拼接
    @ResponseBody
    public String json1() throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        User user = new User("郑冲冲",20,"男");
        return objectMapper.writeValueAsString(user);
    }

    //地址
    @RequestMapping("/j2")
    //返回字符串，不走视图解析器，不会拼接
    @ResponseBody
    public String json2() throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();

        return objectMapper.writeValueAsString(getUserList());
    }

    @RequestMapping("/j3")
    @ResponseBody
    public String json3() throws JsonProcessingException {
        Date date = new Date();
        return JsonUtils.getJson(date,"yyyy-MM-dd HH:mm:ss");
    }

    @RequestMapping(("/j4"))
    @ResponseBody
    public String json4() {
        return JSON.toJSONString(getUserList());
    }

    private List<User> getUserList() {
        List<User> users = new ArrayList<>();
        User user1 = new User("郑冲冲1",20,"男");
        User user2 = new User("郑冲冲2",20,"男");
        User user3 = new User("郑冲冲3",20,"男");
        User user4 = new User("郑冲冲4",20,"男");
        users.add(user1);
        users.add(user2);
        users.add(user3);
        users.add(user4);
        return users;
    }
}
